﻿Ext.define('Examples.tree.menuitem.view.Form', {
    extend: 'Happy.form.Panel',
    alias: 'widget.menuitem-form',
    requires: [
        'Examples.tree.menuitem.store.Store'
    ],

    initComponent: function () {
        var me = this;

        me.items = [
            {
                fieldLabel: 'Name',
                name: 'Name',
                allowBlank: false
            },
            {
                xtype: 'combobox',
                fieldLabel: 'Parent',
                name: 'ParentId',
                displayField: 'Name',
                valueField: 'Id',
                store: Ext.create('Examples.tree.menuitem.store.Store', {
                    autoLoad: true,
                    listeners: {
                        load: function () {
                            this.insert(0, this.getProxy().getModel().create({ Id: '\\', Name: '根节点' }));
                        }
                    }
                }),
                queryMode: 'local',
                tpl: Ext.create('Ext.XTemplate',
                    '<tpl for=".">',
                        '<div class="x-boundlist-item">{[this.tree(values)]}</div>',
                    '</tpl>', {
                        tree: function (values) {
                            var level = 0;
                            if (values.Id != '\\') {
                                level = values.Id.split('\\').length - 2;
                            }

                            return Ext.String.repeat('&nbsp;', 4 * level) + values.Name;
                        }
                    }
                )
            }
        ];

        me.buttons = Happy.createActions([
            'save',
            'close'
        ]);

        me.callParent(arguments);
    }
});